<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace House\Controller;
use Home\Controller\HomeController;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController{

    //系统首页
    public function index($area=0,$price=0,$style=0,$orientation=0,$mold=0,$page=1){


       //获得地区表数据
       $model1=D('house_area');
       $arealist=$model1->order('id asc')->select();
        $this->assign('area1',$arealist);

        //获得租金表数据
        $model2=D('house_price');
        $pricelist=$model2->order('id asc')->select();
        $this->assign('price1',$pricelist);

        //获得出租方式表数据
        $model3=D('house_style');
        $stylelist=$model3->order('id asc')->select();
        $this->assign('style1',$stylelist);

        //获得出租朝向表数据
        $model4=D('house_orientation');
        $orientationlist=$model4->order('id asc')->select();
        $this->assign('orientation1',$orientationlist);

        //获得房屋类型表数据
        $model5=D('house_mold');
        $moldlist=$model5->order('id asc')->select();
        $this->assign('mold1',$moldlist);

        $map_area = getName($area,'area');
        $map_price = getName($price,'price');


       if($map_price == '600以下'){
           $map_price=array(0=>0,1=>600);
       } else{
           $map_price = explode('-',$map_price);
       }



        $map_style = getName($style,'style');
        $map_orientation = getName($orientation,'orientation');
        $map_mold = getName($mold,'mold');

        $area && $map['position'] = $map_area;
        $price && $map['price'] = array('between',array($map_price[0],$map_price[1]));
        $style &&  $map['style'] = $map_style;
        $orientation &&  $map['orientation'] = $map_orientation;
        $mold &&  $map['mold'] = $map_mold;

     if($map_price[0] == '2000以上'){
            $map['price'] = array('gt',2000);

        }

        $Member = M('release_info');
        $c=15;


        if(!empty($_POST['search'])){
            $model=D('release_info');
            $v =trim( $_POST['search']);
            $this->assign('search',$v);
            $lists = $model->where('title  like "%' . $v . '%" OR village  like "%' . $v . '%" OR description  like "%' . $v . '%"')->order('time desc')->page($page, $c)->select();
            $totalCount = $model->where('title like "%' . $v . '%" OR village  like "%' . $v . '%" OR description  like "%' . $v . '%"')->order('time desc')->count();
        }
        else{
            $lists = $Member->where($map)->order('time desc')->page($page, $c)->select();
            $totalCount = $Member->where($map)->order('time desc')->count();
        }



        $this->assign('pagenum',$c);

        $this->assign('lists',$lists);
        $this->assign('totalCount', $totalCount);
        $this->assign('area',$area);
        $this->assign('style',$style);

        $this->assign('mold',$mold);
        $this->assign('orientation',$orientation);
        $this->assign('price',$price);
        $this->assign('page',$page);
        $this->assign('tab',"zufang");
        $this->display();
    }
    //求租页面
    public function qiuzu($page=1){
        //获取记录条数
      /*  $model = D('qiuzu_info');
       /* $totalCount= $model->count();
        $this->assign('totalCount',"$totalCount");*/

        //获取求租表数据
       $Member = M('qiuzu_info');
        $c=20;
        if(!empty($_POST['search'])){
            $model=D('qiuzu_info');
            $v =trim( $_POST['search']);
            $this->assign('search',$v);
            $lists = $model->where('title  like "%' . $v . '%" OR village  like "%' . $v . '%" OR description  like "%' . $v . '%"')->order('time desc')->page($page, $c)->select();
            $totalCount = $model->where('title like "%' . $v . '%" OR village  like "%' . $v . '%" OR description  like "%' . $v . '%"')->order('time desc')->count();
        }
        else{
            $lists = $Member->order('time desc')->page($page, $c)->select();
            $totalCount = $Member->order('time desc')->count();
        }

        $this->assign('pagenum',$c);

        $this->assign('lists',$lists);



        $this->assign('tab',"qiuzu");

        $this->assign('totalCount', $totalCount);
        $this->display();

    }
    public function zixun(){

        $this->assign('tab',"zixun");

        $this->display();

    }
    public function  re_chuzu(){

        if (!is_login()) {
            //没发生过
            $this->error('用户未登录，请先登录！',U('/Home/User/login'));
        } else {
            //发生过返回
            $this->display();
        }

    }
    public function  re_qiuzu(){

        if (!is_login()) {
            //没发生过
            $this->error('用户未登录，请先登录！',U('/Home/User/login'));
        } else {
            //发生过返回
            $this->display();
        }

    }
    public function  qiuzu_detail($id){
        $map['id']=$id;
        $detail = D('qiuzu_info')->where($map)->find();
        $this->assign('detail',$detail);
        D('qiuzu_info')->where('id='.$id)->setInc('browse');
        $this->display();


    }
    public function  chuzu_detail($id){
        $map['id']=$id;
        $detail = D('release_info')->where($map)->find();
        $this->assign('detail',$detail);
         D('release_info')->where('id='.$id)->setInc('browse');
        $this->display();


    }
    public function dore_chuzu($position,$orientation,$village,$title,$nickname,$phone,$description,$price,$type,$area,$photo,$style,$configure,$mold){


        /*判断表单输入是否为空*/
        empty($style) && $this->error('请选择出租形式');
        empty($position) && $this->error('请填写地区');
        empty($village) && $this->error('请填写小区');
        empty($type) && $this->error('请填写户型');
        empty($area) && $this->error('请填写面积');
        empty($price) && $this->error('请填写租金');
        empty($orientation) && $this->error('请选择朝向');
        empty($mold) && $this->error('请选择房屋类型');
        empty($configure) && $this->error('请填写配置');
        empty($title) && $this->error('请填写标题');
        empty($description) && $this->error('请填写描述');
        empty($nickname) && $this->error('请填写联系人');
        empty($phone) && $this->error('请填写联系电话');

        preg_match_all('/\[(.*?)\]/',$photo,$photo1);
        /*判断照片至少是3张*/
       count($photo1[1]) < 3 && $this->error('请至少上传3张照片');
        /*获取数据表字段对应值*/
        $user_auth=session('user_auth');
        $data['uid']=$user_auth['uid'];
       $data['style']=$_POST['style'];
        $data['position']=$position;
        $data['orientation']=$orientation;
        $data['village']=$village;
        $data['title']=$title;
        $data['nickname']=$nickname;
        $data['price']=$price;
        $data['mold']=$mold;
        $data['type']=$type;
        $data['configure']=$configure;
        $data['photo']=$photo;
        $data['description']= str_replace(array("\n" , " " ) , array("<br>","&nbsp;") ,$description);
        $data['phone']=$phone;
        $data['area']=$area;
        $data['time']=time();




        /*写入数据表*/
       $res =  D('release_info')->add($data);



    if($res){


    $this->success('发布成功',U('House/index/index'));

}
    else{
        $this->error('发布失败',U('House/index/re_chuzu'));
    }

    }






    public function uploadimage(){

        $this->display();
    }


    public function upload(){

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =      'Picture/'; // 设置附件上传目录
        // 上传文件
        $info   =   $upload->upload();
        $info['img']['path'] = __ROOT__.'/Uploads/'.$info['img']['savepath'].$info['img']['savename'];
        echo json_encode($info);

    }


    public function dore_qiuzu($position,$village,$title,$nickname,$phone,$description,$price_min,$price_max,$type){



        empty($position) && $this->error('请填写地区');

        empty($type) && $this->error('请填写户型');

        empty($price_min) && $this->error('请填写最小租金');
        empty($price_max) && $this->error('请填写最大租金');

        empty($title) && $this->error('请填写标题');
        empty($description) && $this->error('请填写描述');
        empty($nickname) && $this->error('请填写联系人');
        empty($phone) && $this->error('请填写联系电话');





        $user_auth=session('user_auth');
        $data['uid']=$user_auth['uid'];
        $data['position']=$position;

        $data['village']=$village;
        $data['title']=$title;
        $data['nickname']=$nickname;
        $data['price_min']=$price_min;
        $data['price_max']=$price_max;
        $data['type']=$type;
        $data['description']=str_replace(array("\n" , " " ) , array("<br>","&nbsp;") ,$description);
        $data['phone']=$phone;


        $data['time']=time();

        D('qiuzu_info')->add($data);
        $this->success('发布求租成功',U('House/Index/qiuzu'));
    }







}